<?php

/* Interfaces */
/*
require_once('classes/IAuthenticationAdapter.php');
*/

/* Implementations (replace with interfaces/factories) */
require_once('classes/MockAuthenticationAdapter.php');

require_once('classes/Authenticator.php');
require_once('classes/WebService.php');

class AuthenticationService extends WebService {
	
	private $authenticator;
	private $authAdapter;
	
	public function __construct(IRequestHandler $requestHandler = null, IResponseHandler $responseHandler = null) {
		parent::__construct($requestHandler, $responseHandler);
		
		$this->authAdapter = new MockAuthenticationAdapter();
		$this->authenticator = new Authenticator($this->authAdapter);
	}
	
	public function authenticate($request) {
		$token = '';
		
		try {
			$token = $this->authenticator->authenticate($request->username, $request->password);
			$response = array(
				'success'=>true,
				'token'=>$token,
				'msg'=>''
			);
		} catch (SecurityException $e) {
			$response = array(
				'success'=>false,
				'token'=>$token,
				'msg'=>$e->getMessage()
			);
		}	
		
		$this->send($response);
	}
	
	public function checkToken($request) {
		$this->authenticator->checkToken($request->token);
	}
	
}

$service = new AuthenticationService();
$service->listen();

?>